﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

using Gurucore.Framework.Core.Proxy;

namespace Gurucore.Framework.Business.TransactionContext
{
	public class TransactionalServiceProxier : DynamicProxier
	{
		protected override List<InterceptorInfo> AnalyzeMethod(MethodInfo p_oMethod)
		{
			const string FRAMEWORK_ASSEMBLY = "Gurucore.Framework.dll";
			List<InterceptorInfo> arrResult = base.AnalyzeMethod(p_oMethod);
			object[] arrDataSourceAttr = p_oMethod.GetCustomAttributes(typeof(DataSourceAttribute), true);
			object[] arrTransactionBoundAttr = p_oMethod.GetCustomAttributes(typeof(TransactionBoundAttribute), true);

			if ((p_oMethod.IsVirtual) && ((arrDataSourceAttr.Length > 0) || (arrTransactionBoundAttr.Length > 0)))
			{
				InterceptorInfo oBeforeInt = new InterceptorInfo();
				oBeforeInt.Assembly = FRAMEWORK_ASSEMBLY;
				oBeforeInt.Class = typeof(BeforeInterceptor).FullName;
				oBeforeInt.Type = InterceptorType.BeforeCalling;
				arrResult.Add(oBeforeInt);

				InterceptorInfo oAfterSuccessInt = new InterceptorInfo();
				oAfterSuccessInt.Assembly = FRAMEWORK_ASSEMBLY;
				oAfterSuccessInt.Class = typeof(AfterSuccessInterceptor).FullName;
				oAfterSuccessInt.Type = InterceptorType.AfterCallSuccess;
				arrResult.Add(oAfterSuccessInt);

				InterceptorInfo oAfterFailureInt = new InterceptorInfo();
				oAfterFailureInt.Assembly = FRAMEWORK_ASSEMBLY;
				oAfterFailureInt.Class = typeof(AfterFailureInterceptor).FullName;
				oAfterFailureInt.Type = InterceptorType.AfterCallFailure;
				arrResult.Add(oAfterFailureInt);

				InterceptorInfo oAfterReturnedInt = new InterceptorInfo();
				oAfterReturnedInt.Assembly = FRAMEWORK_ASSEMBLY;
				oAfterReturnedInt.Class = typeof(AfterReturnedInterceptor).FullName;
				oAfterReturnedInt.Type = InterceptorType.AfterReturned;
				arrResult.Add(oAfterReturnedInt);
			}

			return arrResult;
		}
	}
}
